package com.lsy.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.lsy.entity.ProductCustom;
import com.lsy.entity.ProductQueryVo;
import com.lsy.mapper.interf.ProductMapperCustom;
import com.lsy.service.interf.ProductService;

/**
 * service必须要加上Service注解
 * 
 * @author Administrator
 *
 */
@Service
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProductServiceImpl implements ProductService {

	private final int pageNumber = 20;

	@Autowired
	private ProductMapperCustom productMapperCustom;

	public List<ProductCustom> findproductList(ProductQueryVo productQueryVo) throws Exception {

		// 通过ProductMapperCustom查询数据库
		return productMapperCustom.findIproductsList(productQueryVo);
	}

	// 保存数据
	public boolean save(ProductCustom object) throws RuntimeException {
		boolean result = false;
		if (productMapperCustom.insert(object) > 0) {
			result = true;
		}
		return result;
	}

	// 散标分页
	public List<ProductCustom> findProductByPage(int page, int total) throws Exception {
		int start = pageNumber * (page - 1);
		if (start >= total) {
			return new ArrayList<ProductCustom>();
		}
		return productMapperCustom.findProductByPage(start, pageNumber);
	}

	// 散标总数
	public int findTotal() throws Exception {
		return productMapperCustom.findTotal();
	}

	// 根据id查询散标
	public ProductCustom findProductById(int id) throws Exception {
		return productMapperCustom.findProductById(id);
	}

	// 更新散标信息
	public boolean updateProduct(ProductCustom product, boolean isComplete) throws Exception {
		boolean result = false;
		if (productMapperCustom.updateProduct(product, isComplete) > 0) {
			result = true;
		}
		return result;
	}

	// 获取分页数
	public int getPageNumber() {
		return this.pageNumber;
	}

	public void delete(int id) {
		productMapperCustom.delete(id);
	}

	public void update(ProductCustom product) throws Exception {
		productMapperCustom.update(product);
	}

}
